//
//  CoinSprite.m
//  StarBugs
//
//  Created by ?? ? on 12/28/11.
//  Copyright 2011 antisword@playorca.com. All rights reserved.
//

#include "Object/CoinSprite.h"
#include "Util/ImageManager.h"
#include "Util/Util.h"
#include "Layer/GameUILayer.h"
#include "Util/ModuleController.h"

CoinSprite::~CoinSprite()
{

//    if(frameArray)
//    {
//        frameArray->removeAllObjects();
//        frameArray->release();
//        frameArray = NULL;
//    }
//    
////    if(mainSprite)
////    {
////        mainSprite->removeAllChildrenWithCleanup(true);
////        mainSprite->release();
////        mainSprite = NULL;
////    }
}

CoinSprite::CoinSprite()
{
//    mainSprite = ImageManager::getSpriteWithXml2(kImagesheet_GameSource, "star1");
    mainSprite = new CCSprite;
    mainSprite->init();
    mainSprite->autorelease();
    mainSprite->setScale(0.8f);
    addChild(mainSprite);
    
    objectType = objectType_coin;
    
    char txt[12];
    for(int i = 0; i < 12; i++)
    {
        sprintf(txt, "energy_%d", i + 1);
        setFrame(0, ImageManager::getFrameWithXml2(kImagesheet_GameSource, txt));
    }
//    startAnimation();
}

void CoinSprite::setBody(b2World* _world)
{
    mainSprite->setPosition(position);
}

void CoinSprite::eatCoin(b2World *_world)
{   

    CCPoint goalPos = ccp((winSize.width - 40)*ModuleController::getImageScale(), 40*ModuleController::getImageScale());
    float time = ccpDistance(goalPos , mainSprite->getPosition()) / 300.f;
    float angle = Util::degreeTwoPoint(goalPos, mainSprite->getPosition());
    int _x = 50 *ModuleController::getImageScale() * +sin(CC_DEGREES_TO_RADIANS(angle));
    int _y = 50 *ModuleController::getImageScale() * -cos(CC_DEGREES_TO_RADIANS(angle));
    
    
    
    CCPoint pt = ccpRotateByAngle(mainSprite->getPosition(), ccp(mainSprite->getPosition().x + _x, mainSprite->getPosition().y + _y), CCRANDOM_0_1() * 2);
    CCMoveTo *move1 = CCMoveTo::create(0.3, pt);
    CCEaseOut *accel1 = CCEaseOut::create(move1, 3);
    
    CCMoveTo *move2 = CCMoveTo::create(time, goalPos);
    CCEaseIn *accel2 = CCEaseIn::create(move2, 3);
    
    CCMoveTo *move3 = CCMoveTo::create(0.2, ccp(goalPos.x, goalPos.y + 20*ModuleController::getImageScale()));
    CCFadeOut *fadeout = CCFadeOut::create(0.2);
    CCCallFunc *call = CCCallFunc::create(this, callfunc_selector(CoinSprite::eatCall));
    
    CCAction *action = CCSequence::create(accel1, accel2, move3,  fadeout, call, NULL);
    mainSprite->runAction(action);
}

void CoinSprite::eatCall()
{
//    worldTmp->DestroyBody(mainBody);
//    mainBody = NULL;   
//    removeFromParentAndCleanup(true);
//    GameUILayer::getInstancePtr()->setScorePlus(100);
}

